#include "GlobalHead.h"
#include "Command.h"
#include "Action.h"
#include "Workbench.h"
#include "WorkbenchManager.h"
#include "MainWindow.h"
#include "BitmapFactory.h"
#include "ViewProvider.h" // 
#include "ViewProviderDocumentObject.h" //
#include "sceneObject.h"
#include "Gui_Application.h"
#include "View3DInventor.h"
#include "VtRender.h"
#include "FullScreen.h"
#include "MultiScreen.h"
#include "ShadowCaster.h"
#include "Statistics/Statistics.h"

using namespace Gui;

DEF_STD_CMD_AC(StdCmdWorkbench);

StdCmdWorkbench::StdCmdWorkbench()
:Command("Std_Workbench")
{
	sGroup        = QT_TR_NOOP("View");
	sMenuText     = QT_TR_NOOP("Workbench");
	sToolTipText  = QT_TR_NOOP("Switch between workbenches");
	sWhatsThis    = "Std_Workbench";
	sStatusTip    = QT_TR_NOOP("Switch between workbenches");
	sPixmap       = "freecad";
//	iAccel        = 0;
	eType         = 0;
}

void StdCmdWorkbench::activated(int i)
{
	Workbench* w = WorkbenchManager::instance()->active();
	QList<QAction*> items = static_cast<WorkbenchGroup*>(_pcAction)->actions();
	std::string switch_to = (const char*)items[i]->objectName().toAscii();
	if (w) {
		std::string current_w = w->name();
		if (switch_to == current_w)
			return;
	}
	//doCommand(Gui, "Gui.ActivateWorkbench", switch_to.c_str());
	doCommand((CommandStringFunction)Gui_Application::ActivateWorkbench,switch_to.c_str());
}
bool StdCmdWorkbench::isActive(void)
{
	return true;
}

Action* StdCmdWorkbench::createAction(void)
{
	Action *pcAction;

	pcAction = new WorkbenchGroup(this,getMainWindow());
	applyCommandData(pcAction);
	if (sPixmap)
		pcAction->setIcon(BitmapFactory().pixmap(sPixmap));
	pcAction->setShortcut(QString::fromAscii(sAccel));

	return pcAction;
}


//===========================================================================
// Std_About
//===========================================================================

DEF_STD_CMD_ACL(StdCmdAbout)

StdCmdAbout::StdCmdAbout()
  :Command("Std_About")
{
    sGroup        = QT_TR_NOOP("Help");
    sMenuText     = QT_TR_NOOP("&About %1");
    sToolTipText  = QT_TR_NOOP("About %1");
    sWhatsThis    = "Std_About";
    sStatusTip    = QT_TR_NOOP("About %1");
    eType         = 0;
}

Action * StdCmdAbout::createAction(void)
{
    Action *pcAction;

     QString exe = QString::fromUtf8(GetApp_Application().getExecutableName());
     pcAction = new Action(this,getMainWindow());
     pcAction->setText(QCoreApplication::translate(
         this->className(), sMenuText, 0,
         QCoreApplication::CodecForTr).arg(exe));
     pcAction->setToolTip(QCoreApplication::translate(
         this->className(), sToolTipText, 0,
         QCoreApplication::CodecForTr).arg(exe));
     pcAction->setStatusTip(QCoreApplication::translate(
         this->className(), sStatusTip, 0,
         QCoreApplication::CodecForTr).arg(exe));
     pcAction->setWhatsThis(QLatin1String(sWhatsThis));
     pcAction->setIcon(QApplication::windowIcon());
     pcAction->setShortcut(QString::fromAscii(sAccel));

    return pcAction;
}

bool StdCmdAbout::isActive()
{
    return true;
}

/**
 * Shows information about the application.
 */
void StdCmdAbout::activated(int iMsg)
{
//     Gui::Dialog::AboutDialog dlg( getMainWindow() );
//     dlg.exec();
}

void StdCmdAbout::languageChange()
{
     if (_pcAction) {
         QString exe = QString::fromUtf8(GetApp_Application().getExecutableName());
         _pcAction->setText(QCoreApplication::translate(
             this->className(), sMenuText, 0,
             QCoreApplication::CodecForTr).arg(exe));
         _pcAction->setToolTip(QCoreApplication::translate(
             this->className(), sToolTipText, 0,
             QCoreApplication::CodecForTr).arg(exe));
         _pcAction->setStatusTip(QCoreApplication::translate(
             this->className(), sStatusTip, 0,
             QCoreApplication::CodecForTr).arg(exe));
         _pcAction->setWhatsThis(QLatin1String(sWhatsThis));
     }
}

//===========================================================================
// Std_AboutQt
//===========================================================================
DEF_STD_CMD(StdCmdAboutQt);

StdCmdAboutQt::StdCmdAboutQt()
  :Command("Std_AboutQt")
{
  sGroup        = QT_TR_NOOP("Help");
  sMenuText     = QT_TR_NOOP("About &Qt");
  sToolTipText  = QT_TR_NOOP("About Qt");
  sWhatsThis    = "Std_AboutQt";
  sStatusTip    = QT_TR_NOOP("About Qt");
  eType         = 0;
}

void StdCmdAboutQt::activated(int iMsg)
{
  qApp->aboutQt();
}


//===========================================================================
// Std_DlgPreferences
//===========================================================================
DEF_STD_CMD(StdCmdDlgPreferences);

StdCmdDlgPreferences::StdCmdDlgPreferences()
:Command("Std_DlgPreferences")
{
	sGroup        = QT_TR_NOOP("Tools");
	sMenuText     = QT_TR_NOOP("&Preferences ...");
	sToolTipText  = QT_TR_NOOP("Opens a Dialog to edit the preferences");
	sWhatsThis    = "Std_DlgPreferences";
	sStatusTip    = QT_TR_NOOP("Opens a Dialog to edit the preferences");
	sPixmap     = "preferences-system";
	//iAccel        = 0;
	eType         = 0;
}

void StdCmdDlgPreferences::activated(int iMsg)
{
// 	Gui::Dialog::DlgPreferencesImp cDlg(getMainWindow());
// 	cDlg.exec();
}

DEF_STD_CMD(StdCmdOnlineHelp);

StdCmdOnlineHelp::StdCmdOnlineHelp()
:Command("Std_OnlineHelp")
{
	sGroup        = QT_TR_NOOP("Help");
	sMenuText     = QT_TR_NOOP("Help");
	sToolTipText  = QT_TR_NOOP("Show help to the application");
	sWhatsThis    = "Std_OnlineHelp";
	sStatusTip    = QT_TR_NOOP("Help");
	sPixmap       = "help-browser";
	sAccel        = keySequenceToAccel(QKeySequence::HelpContents);
	eType         = 0;
}

void StdCmdOnlineHelp::activated(int iMsg)
{
	getMainWindow()->showDocumentation();
}

//////////////////////////////////////////////////////////////////////////

DEF_STD_CMD(StdCmdFullScreen);

StdCmdFullScreen::StdCmdFullScreen()
:Command("Std_ViewScreenShot")
{
	sGroup        = QT_TR_NOOP("viewNums");
	sMenuText     = QT_TR_NOOP("show full screen");
	sToolTipText  = QT_TR_NOOP("show full screen");
	sWhatsThis    = "Std_ViewScreenShot";
	sPixmap       = "Std_ViewScreenShot";
	sStatusTip    = QT_TR_NOOP("Std_ViewScreenShot");
	//eType         = 0;
}

void StdCmdFullScreen::activated(int iMsg)
{
	//...Make the sub-window full screen.
	FullScreen::do_fullScrren();
}

//////////////////////////////////////////////////////////////////////////

DEF_STD_CMD_C(CmdRenderView);

CmdRenderView::CmdRenderView()
: Command("RenderView")
{
	sAppModule      = "viewNums";
	sGroup          = QT_TR_NOOP("viewNums");
	sMenuText       = QT_TR_NOOP("Render view");
	sToolTipText    = QT_TR_NOOP("Render view stuff");
	sStatusTip      = sToolTipText;
}

void CmdRenderView::activated(int iMsg)
{
	// ...Do the actual work here
	MultiScreen::do_multiScreen(iMsg);
}

Action * CmdRenderView::createAction(void)
{
	QComboBox* cb = new QComboBox();
	cb->addItem(QString::fromAscii("views-one"));
	cb->addItem(QString::fromAscii("views-two"));
	cb->addItem(QString::fromAscii("views-three"));
	cb->addItem(QString::fromAscii("views-four"));
	QWidgetAction* wa = new QWidgetAction(0);
	wa->setDefaultWidget(cb);
	ActionGroup* pcAction = new ActionGroup(this);
	pcAction->addAction(wa);
	QObject::connect(cb, SIGNAL(activated(int)), pcAction, SLOT(onActivated(int)));
	return pcAction;
}

// ------------------- [12/17/2012 Administrator]
DEF_STD_CMD(StdCmdCalculateShadow);
StdCmdCalculateShadow::StdCmdCalculateShadow()
: Command("Std_CalculateShadow")
{
	sGroup        = QT_TR_NOOP("viewNums");
	sMenuText     = QT_TR_NOOP("Fit selection");
	sToolTipText  = QT_TR_NOOP("Fits the selected content on the screen");
	sWhatsThis    = "Std_CalculateShadow";
	sStatusTip    = QT_TR_NOOP("Fits the selected content on the screen");
	sPixmap       = "preferences-raytracing";
}
void StdCmdCalculateShadow::activated(int iMsg)
{
	ShadowCaster::Instance()->show();
}

////////////////////////////////////////////////////////////////
DEF_STD_CMD(StdCmdStatistics)
StdCmdStatistics::StdCmdStatistics()
:Command("Std_Statistics")
{
	sGroup        = QT_TR_NOOP("viewNums");
	sMenuText     = QT_TR_NOOP("Statistics");
	sToolTipText  = QT_TR_NOOP("Statistics");
	sWhatsThis    = "Std_Statistics";
	sStatusTip    = QT_TR_NOOP("Statistics");
	sPixmap       = "Tree_Annotation";
}

void StdCmdStatistics::activated(int iMsg)
{
	App::DocumentObject *editObject = NULL;

	Gui::Gui_Document *doc = Gui_Application::instance->activeDocument();
	if(doc)
	{
		if(doc->getInEdit() && doc->getInEdit()->isDerivedFrom(Gui::ViewProviderDocumentObject::getClassTypeId()))
		{
			Gui::ViewProviderDocumentObject* vp =static_cast<Gui::ViewProviderDocumentObject*>(doc->getInEdit());
			if(CCustomPlayer::Instance().isInActViewport(vp->getEntity()) == true)
				editObject =  vp->pcObject;
		}	

	}
	if(editObject == NULL)
		return;

	if(editObject->getTypeId().isDerivedFrom(Part::sceneObject::getClassTypeId()))
	{
		Part::sceneObject* ps = static_cast<Part::sceneObject*>(editObject);
		CK3dEntity* cb = ps->Shape.getValue();
	/*	if(cb)	
		{
			CCustomPlayer::Instance().addPostRenderCallBack(CK_RENDERCALLBACK(Gui::ViewProvider::eventCallback),(void*)cb);
		}*/
		Statistics::Instance()->do_superficial_area(cb);
		Statistics::Instance()->showDialog();
	}

	//Statistics::Instance()->do_superficial_area()

}

void CreateStdCommands(void)
{
	CommandManager &rcCmdMgr = Gui_Application::instance->commandManager();

	rcCmdMgr.addCommand(new StdCmdAbout());
	rcCmdMgr.addCommand(new StdCmdAboutQt());
	rcCmdMgr.addCommand(new StdCmdDlgPreferences());

    rcCmdMgr.addCommand(new StdCmdOnlineHelp());
	rcCmdMgr.addCommand(new StdCmdWorkbench());
	rcCmdMgr.addCommand(new CmdRenderView());
	rcCmdMgr.addCommand(new StdCmdFullScreen());
	rcCmdMgr.addCommand(new StdCmdCalculateShadow());
	rcCmdMgr.addCommand(new StdCmdStatistics());
}
